/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.NetworkManagement;

import Utilities.Constants;
import Utilities.Functions;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * modify a host interface is sent to the networks management service.
 *
 * @author Angel Sanadinov
 */
public class ModifyHostInterfaceRequest extends NetworkManagementRequest
{
    private int newOwnerId;                             //the id of the new interface owner
    private String permissions;                         //the new permissions
    private String staticIPAddress;                     //static IPv4/IPv6 address
    private String staticIPv4Mask;                      //static IPv4 network mask
    private long staticIPv6MaskPrefixLength;            //static IPv6 mask prefix length

    private boolean arePermissionsSet;                  //denotes whether new permissions were set
    private boolean isNewOwnerIdSet;                    //denotes whether a new owner id was set
    private boolean isIPConfigurationSet;               //denotes whether a new IP config was set
    private boolean isDynamicIPConfigurationEnabled;    //denotes whether a dynamic IP config was set
    private boolean isIPv6StaticConfigurationEnabled;   //denotes whether IPv6 static config was set
    private boolean executeDHCPRediscover;              //denotes whether DHCP rediscover is to be done

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see Constants#INVALID_USER_ID
     */
    public ModifyHostInterfaceRequest()
    {
        super();
        this.newOwnerId = Constants.INVALID_USER_ID;
        this.staticIPAddress = null;
        this.staticIPv4Mask = null;
        this.staticIPv6MaskPrefixLength = 0;
        this.arePermissionsSet = false;
        this.isNewOwnerIdSet = false;
        this.isIPConfigurationSet = false;
        this.isDynamicIPConfigurationEnabled = false;
        this.isIPv6StaticConfigurationEnabled = false;
        this.executeDHCPRediscover = false;
        setRequestedAction(PermissionsConstants.ACTION_NETWORK_MODIFY_HOST_IF);
    }

    /**
     * Constructs a "modify host interface" request with the specified data.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the interface resides
     * @param interfaceId the id of the host interface
     */
    public ModifyHostInterfaceRequest(int requestorId, int serverId, String interfaceId)
    {
        this();
        setRequestorId(requestorId);
        setServerId(serverId);
        setNetworkId(interfaceId);
    }

    /**
     * Retrieves the new owner id.
     *
     * @return the owner id, if available or <code>INVALID_USER_ID</code> if not
     *
     * @see Constants#INVALID_USER_ID
     */
    public int getNewOwnerId()
    {
        return newOwnerId;
    }

    /**
     * Retrieves the new permissions of the host interface.
     *
     * @return the interface permissions
     */
    public String getPermissions()
    {
        return permissions;
    }

    /**
     * Retrieves the static IPv4 or IPv6 address.
     *
     * @return the static IP address
     */
    public String getStaticIPAddress()
    {
        return staticIPAddress;
    }

    /**
     * Retrieves the static IPv4 network mask.
     *
     * @return the network mask
     */
    public String getStaticIPv4Mask()
    {
        return staticIPv4Mask;
    }

    /**
     * Retrieves the static IPv6 mask prefix length.
     *
     * @return the mask prefix length
     */
    public long getStaticIPv6MaskPrefixLength()
    {
        return staticIPv6MaskPrefixLength;
    }

    /**
     * Returns whether new permissions data was set.
     *
     * @return <code>true</code> if new permissions data was set or <code>false</code>
     *         otherwise
     */
    public boolean arePermissionsSet()
    {
        return arePermissionsSet;
    }

    /**
     * Returns whether a new owner id was set
     *
     * @return <code>true</code> if a new owner id was set or <code>false</code>
     *         otherwise
     */
    public boolean isNewOwnerIdSet()
    {
        return isNewOwnerIdSet;
    }

    /**
     * Returns whether a new IP configuration was set.
     *
     * @return <code>true</code> if a new configuration was set or <code>false</code>
     *         otherwise
     */
    public boolean isIPConfigurationSet()
    {
        return isIPConfigurationSet;
    }

    /**
     * Returns whether a new dynamic IP configuration was set.
     *
     * @return <code>true</code> if a new dynamic configuration was set or
     *         <code>false</code> if it is static
     */
    public boolean isDynamicIPConfigurationEnabled()
    {
        return isDynamicIPConfigurationEnabled;
    }

    /**
     * Returns whether a new IPv6 static configuration was set.
     *
     * @return <code>true</code> if a new static IPv6 configuration was set or
     *         <code>false</code> if it static IPv4 configuration was set
     */
    public boolean isIPv6StaticConfigurationEnabled()
    {
        return isIPv6StaticConfigurationEnabled;
    }

    /**
     * Returns whether DHCP rediscover is to be carried out.
     *
     * @return <code>true</code> if DHCP rediscover is to be preformed or
     *         <code>false</code> otherwise
     */
    public boolean getDHCPRediscoverState()
    {
        return executeDHCPRediscover;
    }

    /**
     * Sets new permissions for the host interface.
     *
     * @param permissions the new permissions data
     */
    public void setPermissions(String permissions)
    {
        this.permissions = permissions;
        arePermissionsSet = true;
    }

    /**
     * Sets the id of the new owner of the interface.
     *
     * @param newOwnerId the new owner id
     */
    public void setNewOwnerId(int newOwnerId)
    {
        this.newOwnerId = newOwnerId;
        isNewOwnerIdSet = true;
    }

    /**
     * Sets the static IPv4 configuration.
     *
     * @param staticIPAddress interface IP address (v4)
     * @param networkMask interface network mask
     */
    public void setIPv4StaticConfiguration(String staticIPAddress, String networkMask)
    {
        this.staticIPAddress = staticIPAddress;
        this.staticIPv4Mask = networkMask;
        isIPConfigurationSet = true;
        isDynamicIPConfigurationEnabled = false;
        isIPv6StaticConfigurationEnabled = false;
    }

    /**
     * Sets the static IPv6 configuration.
     *
     * @param staticIPAddress interface IP address (v6)
     * @param networkMaskPrefixLength interface mask prefix length
     */
    public void setIPv6StaticConfiguration(String staticIPAddress, long networkMaskPrefixLength)
    {
        this.staticIPAddress = staticIPAddress;
        this.staticIPv6MaskPrefixLength = networkMaskPrefixLength;
        isIPConfigurationSet = true;
        isDynamicIPConfigurationEnabled = false;
        isIPv6StaticConfigurationEnabled = true;
    }

    /**
     * Enables dynamic IP configuration for the interface.
     */
    public void enableDynamicIPConfiguration()
    {
        isIPConfigurationSet = true;
        isDynamicIPConfigurationEnabled = true;
    }

    /**
     * Execute DHCP rediscover for the interface. <br><br>
     *
     * <b>Note:</b> <i>For dynamic IP configuration.</i>
     */
    public void executeDHCPRediscover()
    {
        executeDHCPRediscover = true;
    }

    /**
     * Checks the validity of the static configuration.
     *
     * @return <code>true</code> if the configuration is valid or <code>false</code>
     *         otherwise
     */
    private boolean isStaticConfigurationValid()
    {
        if(staticIPAddress != null)
        {
            if(isIPv6StaticConfigurationEnabled && staticIPv6MaskPrefixLength > 0)
                return true;
            else if(!isIPv6StaticConfigurationEnabled && staticIPv4Mask != null)
                return true;
            else
                return false;
        }
        else
            return false;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && isNetworkIdValid()
                && (!arePermissionsSet || Functions.arePermissionsValid(permissions))
                && (!isNewOwnerIdSet || Functions.isUserIdValid(newOwnerId))
                && (!isIPConfigurationSet || (isDynamicIPConfigurationEnabled || isStaticConfigurationValid()))
          )
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (networkId,newOwnerId,permissions,staticIPAddress,
     * staticIPv4Mask,staticIPv6MaskLength,dynamicConfigEnabled,executeDHCPRediscover)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return ModifyHostInterfaceRequest.class.getSimpleName()
                + ": (" + getNetworkId() + "," + newOwnerId + "," + permissions
                + "," + staticIPAddress + "," + staticIPv4Mask
                + "," + staticIPv6MaskPrefixLength + "," + isDynamicIPConfigurationEnabled
                + "," + executeDHCPRediscover + ")";
    }
}
